package cs304.p3.library.controller;

import cs304.p3.library.domain.Librarian;
import cs304.p3.library.exceptions.AbstractLDBException;
import cs304.p3.library.framework.IoC;
import cs304.p3.library.view.LibrarianDialog;

public class LibrarianController extends Controller{
	
	private Librarian lib = new Librarian();
	private LibrarianDialog view;
	
	public void setView(LibrarianDialog view) {
		this.view = view;
	}

	public Librarian getLibrarian(){
		return lib;
	}

	@Override
	public void cleanUp() {
		lib = new Librarian();
	}

	public void performAddBookAction() {
		boolean error = false;
		try {
			if(lib.getCallNumber().isEmpty()){
				IoC.displayUserInvalidInputMessage("Call Number cannot be empty");
			}else if(lib.getIsbn().isEmpty()){
				IoC.displayUserInvalidInputMessage("ISBN cannot be empty");
			}else if(lib.getTitle().isEmpty()){
				IoC.displayUserInvalidInputMessage("Title cannot be empty");
			}else if(lib.getMainAuthor().isEmpty()){
				IoC.displayUserInvalidInputMessage("Main Author cannot be empty");
			}else if(lib.getPublisher().isEmpty()){
				IoC.displayUserInvalidInputMessage("Publisher cannot be empty");
			}else librarianService.addBook(lib.getCallNumber(), lib.getIsbn(), lib.getTitle(), lib.getMainAuthor(), lib.getAuthors(), lib.getPublisher(), lib.getYear(), lib.getSubjects());
		} catch (AbstractLDBException e) {
			error = true;
			e.printStackTrace();
			e.displayError();
		}
		if(!error){
			IoC.displayCompleteMessage("Book added");
			view.clearAddBookFields();
		}
	}

	public void performAddBookCopyAction() {
		boolean error = false;
		String callNumber = lib.getCallNumber();
		try {
			librarianService.addBookCopy(callNumber);
		} catch (AbstractLDBException e) {
			error = true;
			e.displayError();
			e.printStackTrace();
		}
		if(!error)IoC.displayCompleteMessage("Book Copy added");
	}

	public void performCheckOutReportAction() {
		String sub = lib.getReportSubject();
		view.updateCheckoutReport(librarianService.getCheckedOut(sub));
	}

	public void performPopularReportAction() {
		int y = lib.getReportYear();
		int n = lib.getReportNumBooks();
		view.updatePopularReport(librarianService.getPopularBooks(y, n));
	}

}
